package com.tanhua.mangement.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.pojo.Manager;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.mangement.service.ManagerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统架构
 * 朴家龙
 */
@RestController
@RequestMapping("system/users/")
@Slf4j
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    /**
     * 用户基本信息
     * @return
     */
    @PostMapping("profile")
    public ResponseEntity<Manager>baseInfo(){
        try {
            //获取manager对象
            Manager manager = managerService.baseInfo();
            //如果不为空,返回200
            if (ObjectUtil.isNotEmpty(manager)) {
                return ResponseEntity.ok(manager);
            }
        } catch (Exception e) {
            log.error("获取个人信息失败" + e);//打印日志
        }
        //返回500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 用户登陆
     * @param param
     * @return
     */
    @NoAuthorization
    @PostMapping("login")
    public ResponseEntity<Map<String,String>>login(
            @RequestBody Map<String,String> param
    ){
        //根据字段获取信息
        String username = param.get("username");
        String password = param.get("password");
        String verificationCode = param.get("verificationCode");
        String uuid = param.get("uuid");

        try {
            String tokenValue = this.managerService.login(username,password,verificationCode,uuid);
            if (StringUtils.isNotEmpty(tokenValue)){
                Map<String,String> token = new HashMap<>();
                token.put("token",tokenValue);
                return ResponseEntity.ok(token);
            }
        } catch (Exception e) {
            log.error("登录失败!");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户登出
     * @return
     */
    @PostMapping("logout")
    public ResponseEntity<Void> logOut() {
        try {
            managerService.logOut();
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 生成验证码图片
     *
     * @param uuid
     * @param response
     */
    @NoAuthorization
    @GetMapping("verification")
    public void codePic(@RequestParam("uuid") String uuid, HttpServletResponse response) {
        try {
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
            response.setHeader("Pragma", "no-cache");
            response.setContentType("image/jpeg");

            // 生成验证码图片
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);

            String code = lineCaptcha.getCode();

            log.info("生成验证码，uuid = " + uuid + ", code = " + code);

            System.out.println("uuid = " + uuid);
            System.out.println("code = " + code);

            // 将验证码的值存储到redis中
            managerService.codePic(uuid, code);

            // 输出验证码图片
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

